﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EdiNet
{

    public enum SegmentTerminatorSuffix
    {
        None,
        CR,
        LF,
        CRLF
    }
    public enum EdiStandard
    {
        X12,
        EDIFACT
    }
    public class EdiReaderWriterSettings
    {
        public static readonly EdiReaderWriterSettings DefaultEdifact;
        public static readonly EdiReaderWriterSettings DefaultX12;

        public readonly byte DataElementSeparator;
        public readonly byte ComponentSeparator;
        public readonly byte SegmentTerminator;
        public readonly byte RepetitionSeparator;
        public readonly byte ReleaseIndicator;

        public readonly char DataElementSeparatorChar;
        public readonly char ComponentSeparatorChar;
        public readonly char SegmentTerminatorChar;
        public readonly char RepetitionSeparatorChar;
        public readonly char ReleaseIndicatorChar;


        public readonly SegmentTerminatorSuffix Suffix;
        public readonly EdiStandard Standard;
        public readonly Encoding EdiEncoding;

        static EdiReaderWriterSettings()
        {
            DefaultEdifact = GetDefaultEdifact(DefaultEdiEncoding.Encoding);
            DefaultX12 = GetDefaultX12(DefaultEdiEncoding.Encoding);
        }
        public static EdiReaderWriterSettings GetDefaultEdifact(Encoding enc)
        {
            byte[] b = enc.GetBytes(new char[] { '+', '*', ':', '\'', '?' });
            return new EdiReaderWriterSettings(enc, b[0], b[1], b[2], b[3], b[4], SegmentTerminatorSuffix.None, EdiStandard.EDIFACT);
        }

        public static EdiReaderWriterSettings GetDefaultX12(Encoding enc)
        {
            byte[] b = enc.GetBytes(new char[] { '*', '^', ':', '~', '\0' });
            return new EdiReaderWriterSettings(enc, b[0], b[1], b[2], b[3], b[4], SegmentTerminatorSuffix.None, EdiStandard.X12);
        }

        public static EdiReaderWriterSettings CreateFromChars(Encoding enc, char dataElementSeparator, char repetitionSeparator, char componentSeparator, char segmentTerminator, char releaseIndicator, SegmentTerminatorSuffix segmentTerminatorSuffix, EdiStandard standard)
        {
            byte[] b = enc.GetBytes(new char[] { dataElementSeparator, repetitionSeparator, componentSeparator, segmentTerminator, releaseIndicator });
            return new EdiReaderWriterSettings(enc, b[0], b[1], b[2], b[3], b[4], segmentTerminatorSuffix, standard);
        }

        public EdiReaderWriterSettings(Encoding ediEncoding, byte dataElementSeparator, byte repetitionSeparator, byte componentSeparator, byte segmentTerminator, byte releaseIndicator, SegmentTerminatorSuffix segmentTerminatorSuffix, EdiStandard standard)
        {
            EdiEncoding = ediEncoding;
            DataElementSeparator = dataElementSeparator;
            RepetitionSeparator = repetitionSeparator;
            ComponentSeparator = componentSeparator;
            SegmentTerminator = segmentTerminator;
            ReleaseIndicator = releaseIndicator;
            Suffix = segmentTerminatorSuffix;
            Standard = standard;

            char[] c = ediEncoding.GetChars(new byte[] { dataElementSeparator, repetitionSeparator, componentSeparator, segmentTerminator, releaseIndicator });
            DataElementSeparatorChar = c[0];
            RepetitionSeparatorChar = c[1];
            ComponentSeparatorChar = c[2];
            SegmentTerminatorChar = c[3];
            ReleaseIndicatorChar = c[4];

        }

        public bool IsSeparator(byte b)
        {
            return b == DataElementSeparator || b == SegmentTerminator || b == ComponentSeparator;
        }
    }
}
